var app = getApp()
//常量数据配置
var ConstantData = require('../../common/constant_data')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    numPerPage: 1000,
    leseList: [],
    showList: false,
    selectIndex: 0,
    noLoginFlg:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.getNetwork()
    // that.getApageLeaseRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    this.setData({
      leseList:[],
      pageNum:1,
      // selectIndex:0
    })
    this.getApageLeaseRecord(1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
    /**
   * 监听网络状态
   */
  getNetwork: function () {
    var that = this;
    wx.getNetworkType({
      success: function (res) {
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        let networkType = res.networkType
        that.setData({
          networkType: networkType
        })
      }
    })
    if (wx.onNetworkStatusChange){
      wx.onNetworkStatusChange(function (res) {
        console.log(res)
        let networkType = res.networkType
        let isConnected = res.isConnected
        that.setData({
          networkType: networkType,
          isConnected: isConnected
        })
        if (isConnected) {
          that.getApageLeaseRecord()
        }
      })
    }
    
  },
  //获取订单
  getApageLeaseRecord: function (lode) {
    if(app.globalData.loginData && app.globalData.loginData.nophone == 1) {
      //需要重新登录      
      this.setData({
        noLoginFlg: true
      })
      return
    }
    if (!lode) {
      wx.showLoading({
        title: '加载中...',
        mask: true
      });
    }
    var that = this
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease/wxmini/record_list',
        method: 'post',
        header: header,
        data: {
          numPerPage: that.data.numPerPage,
          pageNum: that.data.pageNum
        },
        success: function (res) {
          console.log("userRecordsList-->", res);
          if (res.data.result == 0) {
            var oldList = that.data.leseList || []
            var newList = oldList.concat(res.data.data.data)
            newList = newList.filter(item => {
              return item.orderType != 1000
            })
            if (res.data.data.pageNum * that.data.numPerPage >= res.data.data.total) {
              that.setData({
                loading: false,
                loadingComplete: true,
                leseList: newList,
                selectOrder: newList[that.data.selectIndex]
              });
            } else {
              that.setData({
                loading: true,
                loadingComplete: false,
                leseList: newList,
                selectOrder: newList[that.data.selectIndex]
              });
            }
          } else {
            wx.showToast({
              title: '获取失败'
            })
          }
        },
        complete() {
          getApp().hideLoading()
        }
      })

    })

  },
  //滚动加载下一页
  onBottom: function(){
    var that = this;
    if (that.data.loading && !that.data.loadingComplete) {
      that.setData({
        pageNum: that.data.pageNum + 1, //每次触发上拉事件，把searchPageNum+1  
      });
      that.getApageLeaseRecord();
    }
  },
  callServer: function () {
    wx.makePhoneCall({
      phoneNumber: '4006710500'
    })
  },
  help_open_lock: function () {
    if (!wx.canIUse('web-view')) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
      return
    }
    wx.navigateTo({
      url: '../../pkgBak/pages/help/open_lock/open_lock',
    })
  },
  help_deposit: function () {
    if (!wx.canIUse('web-view')) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
      return
    }
    // wx.navigateTo({
    //   url: '../../pkgBak/pages/help/deposit/deposit',
    // })
    wx.navigateTo({
      url: '../../pkgBak/pages/help/deposit_description/index',
    })
  },
  help_back: function () {
    if (!wx.canIUse('web-view')) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
      return
    }
    wx.navigateTo({
      url: '../../pkgBak/pages/help/back/back',
    })
  },
  help_lease: function () {
    if (!wx.canIUse('web-view')) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
      return
    }
    wx.navigateTo({
      url: '../../pkgBak/pages/help/lease/lease',
    })
  },
  invoiceTip: function () {
    wx.navigateTo({
      url: '../../pages/orderInvoice/orderInvoice',
    })
  },

  clickOnlineBtn: function () {
    app.contacted()
  },

  startmessage: function (res) {
    console.log("企业微信消息-->", res)
  },

  completemessage: function (res) {
    console.log("企业微信完成回调-->", res)
  },

  contacted() { // mark: 进入客服咨询
    app.contacted()
  },
  //费用明细
  toFee:function(){
    wx.navigateTo({
      url: '/pkgNew/pages/feeDetail/index?selectOrder='+`${this.data.selectOrder?encodeURIComponent(JSON.stringify(this.data.selectOrder)):''}`+`&selectIndex=${this.data.selectIndex}`,
    })
  },
  //收费规则
  toRule:function (){
    wx.navigateTo({
      url: '/pkgNew/pages/feeRule/index?selectOrder='+`${this.data.selectOrder?encodeURIComponent(JSON.stringify(this.data.selectOrder)):''}`+`&noLoginFlg=${this.data.noLoginFlg}`,
    })
  },
  //故障上报
  tofault: function () {
    wx.navigateTo({
      url: '/pkgNew/pages/faultReporting/index',
    })
  },
  //意见反馈
  tofeedback: function () {
    wx.navigateTo({
      url: '/pkgNew/pages/opinionFeeback/index'
    })
  },
  //蓝牙和定位
  toBluetooth:function (){
    wx.navigateTo({
      url: '/pkgNew/pages/bluetoothOrPosition/index',
    })
  },
  //点击切换订单
  onChangeOrder: function () {
    this.setData({
      showList: true
    })
  },
  onCloseList: function () {
    this.setData({
      showList: false
    })
  },
  // 点击选择订单
  onOrder: function (e) {
    let that = this
    this.setData({
      selectIndex: e.currentTarget.dataset.index,
      selectOrder: that.data.leseList[e.currentTarget.dataset.index],
      showList: false
    })
  },
})